package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReCameraZone;
import com.iretailer.report.model.CameraZone;
import com.iretailer.report.model.CameraZoneCamera;
import com.iretailer.report.model.CameraZoneMd;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 10:15
 * @Description:
 */
@RestController
public class CameraZoneController extends BaseAction {


    @RequestMapping("/business/CameraZoneByCameraId.action")
    public Map<String, Object> cameraZoneByCameraId(@RequestBody ReCameraZone reCameraZone) {
        List<CameraZone> find = cameraZoneService.list(new QueryWrapper<CameraZone>().lambda().eq(CameraZone::getCameraId, reCameraZone.getCameraId()).orderByAsc(CameraZone::getZone));
        return RMap.ok(find);
    }

    @RequestMapping("/business/selectCameraZoneCamera.action")
    public Map<String, Object> selectCameraZoneCamera(@RequestBody ReCameraZone reCameraZone) {
        List<CameraZoneCamera> find = cameraZoneService.selectCameraZoneByEnabled_MallId(null, reCameraZone.getMallId());
        return RMap.ok(find);
    }

    @RequestMapping("/business/selectCameraZoneByMallId_ZoneClass.action")
    public Map<String, Object> selectCameraZoneByMallId_ZoneClass(@RequestBody ReCameraZone reCameraZone) {
        List<CameraZoneCamera> find = cameraZoneService.selectCameraZoneByMallId_ZoneClass_Enable(reCameraZone.getMallId(), reCameraZone.getZoneClass(), 1);
        return RMap.ok(find);
    }

    @RequestMapping("/business/gridCameraZoneMd.action")
    public Map<String, Object> gridCameraZoneMd(@RequestBody ReCameraZone reCameraZone,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reCameraZone, QueryOption.class);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult result = cameraZoneService.grid(userId, reCameraZone.getEnabled(), reCameraZone.getMallName(), reCameraZone.getZoneClass(), queryOption);
        return RMap.ok(result);
    }


    @RequestMapping("/business/listCameraZoneMd.action")
    public Map<String, Object> listCameraZoneMd(@RequestBody ReCameraZone reCameraZone,HttpServletRequest request) {
        User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reCameraZone, QueryOption.class);
        String userId = user.getId();
        if ("-".equals(user.getRoleId())) {
            userId = null;
        }
        List<CameraZoneMd> result = cameraZoneService.listCameraZone(userId, reCameraZone.getEnabled(), reCameraZone.getMallName(), reCameraZone.getZoneClass(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/business/saveOrUpdateCameraZone.action")
    public Map<String, Object> saveOrUpdateCameraZone(@RequestBody ReCameraZone reCameraZone,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        CameraZone cameraZone = modelMapper.map(reCameraZone, CameraZone.class);
        boolean result = cameraZoneService.saveOrUpdate(cameraZone, user.getName());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/deleteCameraZone")
    public Map<String, Object> deleteCameraZone(@RequestBody List<Long> ids) {
        boolean result = cameraZoneService.removeByIds(ids);
        return RMap.resultBoolean(result);
    }
}
